﻿using System.IO;
using System.Text;
using DGMResourcesTool.Logic.FileUtil;

namespace DGMResourcesTool.Logic.P4Util
{
    public class P4Utility
    {
        private static readonly string P4_Config_Name = "p4_config.txt";
        private static bool _hasInit;

        public static bool InitConfit()
        {
            if (!_hasInit)
            {
                P4ConfigData p4ConfigData = GetP4Config();
                if (p4ConfigData == null)
                {
                    return false;
                }
                P4Shell.Init(p4ConfigData.m_User, p4ConfigData.m_Password, p4ConfigData.m_Port, p4ConfigData.m_WorkSpace);
                _hasInit = true;
                return true;
            }
            return true;
        }

        /// <summary>
        /// 从文件读取p4参数.
        /// </summary>
        private static P4ConfigData GetP4Config()
        {
            string filePath = FileUtility.GetConfigFilePath(P4_Config_Name);
            string[] p4Lines = FileReaderWriterUtility.ReadFile(filePath, Encoding.UTF8);
            if (p4Lines.Length == 4)
            {
                P4ConfigData p4ConfigData = new P4ConfigData();
                p4ConfigData.m_WorkSpace = p4Lines[0].Split('#')[1];
                p4ConfigData.m_Port = p4Lines[1].Split('#')[1];
                p4ConfigData.m_User = p4Lines[2].Split('#')[1];
                p4ConfigData.m_Password = p4Lines[3].Split('#')[1];
                return p4ConfigData;
            }

            return null;
        }

        /// <summary>
        /// 同步到最新.
        /// </summary>
        public static bool SyncP4(string filePath)
        {
            if (!InitConfit())
            {
                return false;
            }

            if (!File.Exists(filePath))
            {
                return false;
            }

            P4Shell.Update(filePath);
            return true;
        }

        /// <summary>
        /// 添加.
        /// </summary>
        public static bool AddP4(string filePath)
        {
            if (!InitConfit())
            {
                return false;
            }

            if (!File.Exists(filePath))
            {
                return false;
            }

            P4Shell.Add(filePath);
            return true;
        }

        /// <summary>
        /// 删除.
        /// </summary>
        public static bool DeleteP4(string filePath)
        {
            if (!InitConfit())
            {
                return false;
            }

            if (!File.Exists(filePath))
            {
                if (!Directory.Exists(filePath))
                {
                    return false;
                }
                // 如果是路径,对路径进行处理.
                if (filePath.EndsWith("\\"))
                {
                    filePath += "...";
                }
                else
                {
                    filePath += "\\...";
                }
            }
//
//            P4Shell.Checkout(filePath);
//            P4Shell.RevertIfUnChanged(filePath);
            P4Shell.Sync(filePath, true);
            P4Shell.Delete(filePath);
            return true;
        }

        /// <summary>
        /// 提交所有修改.
        /// </summary>
        public static bool SubmitAllChange(string description = "")
        {
            if (!InitConfit())
            {
                return false;
            }

            P4Shell.SubmitAllChange(description);
            return true;
        }
    }
}